local wdtx_modskill = fk.CreateSkill {
  name = "#wdtx_modskill",
  tags = { Skill.Compulsory },
}


wdtx_modskill:addEffect(fk.EventPhaseChanging, {
  mute = true,
  priority = 59,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and data.phase == Player.Discard and table.find(player.player_cards[Player.Hand], function(id) return Fk:getCardById(id):getMark("@@staymax") ~= 0 end)
  end,
  on_use = function(self, event, target, player, data)
    data.skipped = true
  end,
})

wdtx_modskill:addEffect(fk.BeforeCardsMove, {
  priority = 0.001,
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(self.name) and player.id > 0 then
      local cards = {}
      for _, move in ipairs(data) do
        if move.specialName == "self_draws" then return false end
        if move.to == player and move.toArea == Card.PlayerHand and move.moveReason == fk.ReasonDraw then
          for _, info in ipairs(move.moveInfo) do
            table.insert(cards, info.cardId)
          end
        end
      end
      if #cards > 0 then
        event:setCostData(self, {card = cards})
        return true
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local Wdtx = room.logic.Wdtx
    local num = #event:getCostData(self).card
    local draw_pile = player:getPile("self_draws")
    local min = math.min(num, #draw_pile)
    if min == 0 then
      return room:doBroadcastNotify("ShowToast","没牌可以抽了")
    elseif num >= #draw_pile then
      room:doBroadcastNotify("ShowToast","牌堆见底")
    end
    local drawcards = table.random(draw_pile,num)
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand and move.moveReason == fk.ReasonDraw then
        local toget = {}
        for i, info in ipairs(move.moveInfo) do
          move.toArea = Card.DiscardPile
          if drawcards[i] then
            table.insert(toget, drawcards[i])
          end
        end
        room:moveCardTo(toget, Card.PlayerHand, player, fk.ReasonDraw, self.name, "self_draws", false, player, move.moveMark)
      end
    end
  end,
})

wdtx_modskill:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill(self.name) then return end
    local todiscards = {}
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          local ownerid = card:getMark("card_owner")
          if player.id == ownerid and player.room:getCardArea(info.cardId) == Card.DiscardPile then
            table.insert(todiscards,info.cardId)
          end
        end
      end
    end
    todiscards = player.room.logic:moveCardsHoldingAreaCheck(todiscards)
    if #todiscards > 0 then
      event:setCostData(self, {card = todiscards})
      return true
    end
    return false
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self).card
    if #cost_data > 0 then
      local Wdtx = room.logic.Wdtx
      Wdtx:checkUpTmpCards(cost_data)
      player:addToPile("self_discs", cost_data, true, self.name)
    end
  end,
})

wdtx_modskill:addEffect(fk.CardUseFinished, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self.name) and player == target and data.card.id ~= 0 and data.card:getMark("@@costcard") ~= 0 and player.room:getCardArea(data.card) == Card.Processing
  end,
  on_refresh = function(self, event, target, player, data)
    player:addToPile("self_cost", {data.card.id}, true, self.name)
  end,
})

-- 处理 TurnEnd 时机的触发效果
wdtx_modskill:addEffect(fk.TurnEnd, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self.name) and player.room.current == target and #target:getPile("self_discs") > 0
  end,
  on_refresh = function(self, event, target, player, data) 
    local room = player.room
    local Wdtx = room.logic.Wdtx
    Wdtx:shuffCards()
  end,
})

-- wdtx_modskill:addEffect(fk.AfterDrawNCards, {
--   can_refresh = function(self, event, target, player, data)
--     return player:hasSkill(self.name) and player == target
--   end,
--   on_refresh = function(self, event, target, player, data)
--     local room = player.room
--     local draw_num = data.n
--     local draw_pile = player:getPile("self_draws")
--     if draw_num > #draw_pile then -- 摸牌阶段抽牌区不够抽
--       local discs_pile = player:getPile("self_discs")
--       if #draw_pile > 0 then
--         room:moveCards({
--           from = player,
--           ids = draw_pile,
--           fromArea = Card.PlayerSpecial,
--           to = player,
--           toArea = Card.PlayerHand,
--           moveReason = fk.ReasonJustMove,
--           proposer = player,
--           skillName = self.name,
--         })
--       end
--       if #discs_pile == 0 then return end
--     else  -- 摸牌阶段抽牌
--       local drawcards = table.random(draw_pile, draw_num)
--       room:moveCards({
--         from = player,
--         ids = drawcards,
--         fromArea = Card.PlayerSpecial,
--         to = player,
--         toArea = Card.PlayerHand,
--         moveReason = fk.ReasonJustMove,
--         proposer = player,
--         skillName = self.name,
--       })
--     end
--   end,
-- })


return wdtx_modskill
